package com.twoGroup.boot.controller;


import com.twoGroup.boot.pojo.*;
import com.twoGroup.boot.service.CommonService;
import com.twoGroup.boot.service.UserService;
import com.twoGroup.boot.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName
 * @Description TODO
 * @Author Xuyujie
 * @Date 2021/6/9 15:40
 * @Version 1.0
 **/
@Controller
@Api(tags = "页面跳转接口")
public class ViewController
{
    public static Logger logger = LoggerFactory.getLogger(ViewController.class);
    @Autowired
    private CommonService commonService;
    @Autowired
    private UserService userService;
    @ApiOperation("跳转至首页")
    @GetMapping("/")
    public String getIndex(HttpSession session, Model model, HttpServletRequest request){
        User user=(User) session.getAttribute("user");
        Cookie[]cookies=request.getCookies();
        if(cookies!=null){
            String account="";
            String password="";
            for(Cookie temp1:cookies){
                if(temp1.getName().equals("email")){
                    account=temp1.getValue();
                    for(Cookie temp2:cookies){
                        if(temp2.getName().equals("password")){
                            password=temp2.getValue();
                        }
                    }
                }
            }
            User users = userService.queryUser(account);
            if(users!=null){
                String DatePassword=users.getPassword();
                DatePassword= UserUtils.md5(DatePassword);
                if(DatePassword.equals(password)){
                    user=users;
                    if(user.getFlag()==2){
                        user=null;
                    }
                }
            }
        }
        if(user==null){
            session.setAttribute("user",new User());
            logger.info("++++++++++++++++++++"+request.getSession().getId());
        }else{
            session.setAttribute("user",user);
        }
        return "PageHkx/home";
    }
    @ApiOperation("获取首页数据")
    @GetMapping("/getIndex")
    @ResponseBody
    public ResultDate getIndexData(){
        Map<String, List> map=new HashMap<>();
        List<String> goodBrand=new ArrayList<>();
        //获取对应品牌下的商品，以goodsBrand0 1 2.。。。。等形式封装
        List<GoodsBrand> goodsBrandList=commonService.queryForAllGoodsBrand();
        List<List<Map<String, Object>>> Brand = new ArrayList<>();
        for(int i=0;i<goodsBrandList.size();i++){
            Brand.add(commonService.queryForGoodsByBrand(goodsBrandList.get(i).getGoodsBrandId()));
        }
        map.put("Brand",Brand);
        //获取对应店铺下的商品
        //获取对应类型下的商品
        List<GoodsType> goodsTypeList=commonService.queryForAllType();
        List<List<Map<String, Object>>> category = new ArrayList<>();
        for(int i=0;i<goodsTypeList.size();i++){
            category.add(commonService.queryForGoodsByTypeId(goodsTypeList.get(i).getGoodsTypeId()));
        }
        map.put("category",category);
        return ResultDate.success("成功！",map);
    }
//    @ApiOperation(value = "通过分页查询显示所有商品")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "pageNow",value = "当前页",required = true),
//            @ApiImplicitParam(name ="pageSize",value = "页面的大小",required = true)
//    })
//    @ResponseBody
//    @GetMapping("queryForGoodsAllByPage")
//    public ResultDate queryForGoodsAllByPage(Integer pageNow, Integer pageSize){
//        Page<Map<String,Object>> page=commonService.queryForAllByPage(pageNow,pageSize);
//        return ResultDate.fail("请求成功！",page);
//    }
//
    @ApiOperation(value = "控制页面跳转至登录页面")
    @GetMapping("User/login")
    public String Login(){
        return "PageHkx/login";
    }
//
//    @ApiOperation(value = "控制页面跳转至个人中心")
//    @GetMapping("User/personCenter")
//    public String PersonCenter(){
//        return "pagesLjy/personFront";
//    }
//
    @ApiOperation(value = "控制页面跳转商家")
    @GetMapping("Merchant/personShopCar")
    public String PersonShopCar(){
        return "PageHkx/shops";
    }

    @ApiOperation(value = "控制页面跳转所有商品页面")
    @GetMapping("allGoods")
    public String AllGoods(){
        return "PageHkx/more";
    }

    @ApiOperation(value = "控制页面跳转商品详情页面")
    @GetMapping("goodsDetail")
    public String goodsDetail(){
        return "PageHkx/goods";
    }
//
//    @ApiOperation(value = "控制页面跳转回到主页")
//    @GetMapping("indexPage")
//    public String IndexPage(){
//        return "pagesFel/index";
//    }
//
//    @ApiOperation(value = "控制页面跳转至搜索结果")
//    @GetMapping("seekResult")
//    public String SeekResult(){
//        return "pagesFel/seekGoodsResult";
//    }
//
    @ApiOperation(value = "控制页面跳转至搜索个人中心购物车")
    @GetMapping("/User/myCarPage")
    public String MyCarPage(){
        return "PageHkx/shopCar";
    }

    @ApiOperation(value = "控制页面跳转至个人中心地址")
    @GetMapping("/addressPage")
    public String AddressPage(){
        return "PageLy/userAddress";
    }

     @ApiOperation(value = "控制页面跳转至用户页面")
    @GetMapping("/User/userInterface")
    public String ViewPage(){
        return "PageLy/userInterface";
    }

    @ApiOperation(value = "控制页面跳转至修改个人信息")
    @GetMapping("/User/userInformation")
    public String NewpwdPage(){
        return "PageLy/userInformation";
    }

    @ApiOperation(value = "控制页面跳转至后台")
    @GetMapping("admin/login")
    public String adminLogin(){
        return "PageLy/Admin";
    }

    @ApiOperation(value = "控制页面跳转至修改密码")
    @GetMapping("User/updatePassword")
    public String updatePassword(){
        return "PageLy/changePassword";
    }
//
//    @ApiOperation(value = "控制页面跳转至persons")
//    @GetMapping("/admin/persons")
//    public String GetPerson(){
//        return "pagesLjy/persons";
//    }
//
//    @ApiOperation(value = "控制页面跳转至商品")
//    @GetMapping("/admin/goods")
//    public String getGoods(){
//        return "pagesLjy/goods";
//    }
//
//    @ApiOperation(value = "控制页面跳转至商品")
//    @GetMapping("/admin/shop")
//    public String getShop(){
//        return "pagesLjy/shop";
//    }
//
//    @ApiOperation(value = "控制页面跳转至商品")
//    @GetMapping("/admin/applygoods")
//    public String getApplyGoods(){
//        return "pagesLjy/applygoods";
//    }
//
//    @ApiOperation(value = "控制页面跳转至商品")
//    @GetMapping("/admin/applystore")
//    public String getApplyStore(){
//        return "pagesLjy/applystore";
//    }
//
//    @ApiOperation(value = "控制页面跳转至商品")
//    @GetMapping("/admin/storer")
//    public String getStorer(){
//        return "pagesLjy/storer";
//    }

    @ApiOperation(value = "控制页面跳转至订单")
    @GetMapping("/User/allGoods")
    public String allGoods(){
        return "PageLy/allGoods";
    }

    @ApiOperation(value = "控制页面跳转至订单")
    @GetMapping("/User/getGoods")
    public String getGoods(){
        return "PageLy/getGoods";
    }

    @ApiOperation(value = "控制页面跳转至订单")
    @GetMapping("/User/sendGoods")
    public String sendGoods(){
        return "PageLy/sendGoods";
    }

    @ApiOperation(value = "控制页面跳转至订单")
    @GetMapping("/User/waitAssess")
    public String waitAssess(){
        return "PageLy/waitAssess";
    }

    @ApiOperation(value = "控制页面跳转至订单")
    @GetMapping("/User/waitPay")
    public String waitPay(){
        return "PageLy/waitPay";
    }

    @ApiOperation(value = "控制页面跳转至用户喜欢")
    @GetMapping("/User/myLike")
    public String myLike(){
        return "PageLy/mylike";
    }

}
